package tankgame06;

import java.io.*;
import java.util.List;
import java.util.Vector;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/28
 * @time 16:48
 */
public class Recorder {
    /**
     * 记录我方击毁敌方坦克数
     */
    private static int allEnemyTankNum = 0;

    /**
     * 从磁盘读取的记录用node来保存
     * node用vector集合来保存
     */
    private static List<Node> nodes = new Vector<>();

    /**
     * 记录敌方坦克
     */
    private static List<EnemyTank> enemyTanks;

    public static void setEnemyTanks(List<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    /**
     * 定义相关的IO对象
     * 写入到文件
     */
    private static FileWriter fw;
    private static BufferedWriter bw;
    private static String recordFile = "src\\myRecord.txt";
    private static BufferedReader br;
    private static FileReader fr;

    public static String getRecordFile() {
        return recordFile;
    }

    public static int getAllEnemyTankNum() {
        return allEnemyTankNum;
    }

    public static void setAllEnemyTankNum(int allEnemyTankNum) {
        Recorder.allEnemyTankNum = allEnemyTankNum;
    }

    /**
     * 读取上盘记录
     */
    public static List<Node> getNodeAndEnemyTankNum(){
        try {
            fr = new FileReader(recordFile);
            br = new BufferedReader(fr);
            //读取第一行，恢复击毁坦克数
            allEnemyTankNum = Integer.parseInt(br.readLine());
            //读取后面的行，恢复还剩下的坦克
            String line = null;
            while ((line = br.readLine())!=null){
                //x,y,direction
                String[] xyd = line.split(" ");
                Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]), Integer.parseInt(xyd[2]));
                nodes.add(node);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return nodes;
    }

    /**
     * 当游戏退出时
     * 我们将allEnemyTankNum写入磁盘
     * 写入敌方坦克的坐标
     */
    public static void keepRecord(){
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(allEnemyTankNum+"\r\n");
            for (EnemyTank enemyTank : enemyTanks) {
                if (enemyTank.isAlive){
                    String record = enemyTank.getX()+" "+enemyTank.getY()+" "+enemyTank.getDirection();
                    bw.write(record+"\r\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 当我方坦克击毁一个敌人坦克
     * 就调用该方法
     */
    public static void addAllEnemyTankNum(){
        allEnemyTankNum++;
    }
}
